<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>微信分享示例</title>
</head>

<body>
    <script type="text/javascript" src="//res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
    <script type="text/javascript">
        // 微信官方文档 https://mp.weixin.qq.com/wiki
        // response 是从后端获取到的数据
        if (wx) {
            wx.config({
                // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                debug: true,
                // 必填，公众号的唯一标识
                appId: response.appId,
                // 必填，生成签名的时间戳
                timestamp: response.timestamp,
                // 必填，生成签名的随机串
                nonceStr: response.nonceStr,
                // 必填，签名
                signature: response.signature,
                // 必填，需要使用的JS接口列表
                jsApiList: [
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage',
                    'onMenuShareQQ',
                    'onMenuShareWeibo',
                    'onMenuShareQZone',
                    'startRecord',
                    'stopRecord',
                    'onVoiceRecordEnd',
                    'playVoice',
                    'pauseVoice',
                    'stopVoice',
                    'onVoicePlayEnd',
                    'uploadVoice',
                    'downloadVoice',
                    'chooseImage',
                    'previewImage',
                    'uploadImage',
                    'downloadImage',
                    'translateVoice',
                    'getNetworkType',
                    'openLocation',
                    'getLocation',
                    'hideOptionMenu',
                    'showOptionMenu',
                    'hideMenuItems',
                    'showMenuItems',
                    'hideAllNonBaseMenuItem',
                    'showAllNonBaseMenuItem',
                    'closeWindow',
                    'scanQRCode',
                    'chooseWXPay',
                    'openProductSpecificView',
                    'addCard',
                    'chooseCard',
                    'openCard'
                ]
            });
            wx.ready(function () {
                // 定义需要分享的内容
                var shareObj = {
                    title: document.title,
                    desc: '这是我们分享的链接的简介啊！',
                    link: location.href,
                    imgUrl: 'http://www.demo.com/demo.jpg',
                };
                // 分享到朋友
                wx.onMenuShareAppMessage({
                    title: shareObj.title,
                    desc: shareObj.desc,
                    link: shareObj.link,
                    imgUrl: shareObj.imgUrl,
                    success: function (res) {
                        // do sth
                    },
                    cancel: function (res) {
                        // do sth
                    }
                });
                // 分享到朋友圈
                wx.onMenuShareTimeline({
                    title: shareObj.title,
                    link: shareObj.link,
                    imgUrl: shareObj.imgUrl,
                    success: function (res) {
                        // do sth
                    },
                    cancel: function (res) {
                        // do sth
                    }
                })
            });
            wx.error(function (res) {
                // do sth
            });
        }
    </script>
</body>

</html>